//
//  ;
//  TourPlay
//
//  Created by dllo on 15/11/23.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "AcitvityTimeCell.h"
#define WIDTH 8
#define HEIGHT 6

@interface AcitvityTimeCell ()

@property (nonatomic, retain) UIView *viewBack;
@property (nonatomic, retain) UIImageView *imageV;
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UIView *view;

@end

@implementation AcitvityTimeCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}
- (void)createSubViews
{
    self.viewBack = [[UIView alloc] initWithFrame:CGRectMake(WIDTH, 0, SCREEN_WIDTH - WIDTH * 2, 50)];
//    self.viewBack.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.9f];
    self.viewBack.backgroundColor = [UIColor whiteColor];
//    self.viewBack.layer.cornerRadius = 4.f;
    [self.contentView addSubview:self.viewBack];
    [_viewBack release];
    
    self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(10, (50 - 16) / 2.f, 16, 16)];
    [self.viewBack addSubview:self.imageV];
    [_imageV release];
    
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(self.imageV.frame.origin.x + self.imageV.frame.size.width + 10, 0, self.viewBack.frame.size.width - (self.imageV.frame.size.width + self.imageV.frame.origin.x + 10 + 20), 49)];
//    self.label.backgroundColor = [UIColor orangeColor];
    self.label.numberOfLines = 0;
    self.label.font = [UIFont systemFontOfSize:16.f];
    self.label.textColor = [UIColor colorWithRed:0.44 green:0.4 blue:0.4 alpha:1];
    [self.viewBack addSubview:self.label];
    [_label release];
    
    self.view = [[UIView alloc] initWithFrame:CGRectMake(10, 49, self.viewBack.frame.size.width - 20, 1)];
    self.view.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
    [self.viewBack addSubview:self.view];
    [_view release];
    
    // 夜间模式
    [self addColorChangedBlock:^{
        
        self.viewBack.nightBackgroundColor = [ColorSingleton shareSingleton].cellColor;
        self.viewBack.normalBackgroundColor = [ColorSingleton shareSingleton].cellColor;
        
        self.view.nightBackgroundColor = [ColorSingleton shareSingleton].cellLineColor;
        self.view.normalBackgroundColor = [ColorSingleton shareSingleton].cellLineColor;
        
    }];
    
}

- (void)setIndexPath:(NSIndexPath *)indexPath timeMod:(ActivityTimeMod *)timeMod
{
    
    if (0 == indexPath.row) {
    
        self.imageV.image = [UIImage imageNamed:@"detail_icon_time@2x.png"];
        self.label.text = timeMod.timestr;
        
        CGRect rect = self.label.frame;
        rect.size.height = [[self class] heightForContent:timeMod.timestr] + 30;
        self.label.frame = rect;
        
//        NSLog(@"------%@", NSStringFromCGRect(self.label.frame));
        
        CGRect viewBackRect = self.viewBack.frame;
        viewBackRect.size.height = self.label.frame.size.height;
        self.viewBack.frame = viewBackRect;
        
        
        // 设置左上角和右上角为圆角
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.viewBack.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(4, 4)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.viewBack.bounds;
        maskLayer.path = maskPath.CGPath;
        self.viewBack.layer.mask = maskLayer;
        [maskLayer release];
        
        
    } else if (2 == indexPath.row) {
        
        self.imageV.image = [UIImage imageNamed:@"detail_icon_money@2x.png"];
        
        self.view.hidden = YES;
        
        self.label.text = timeMod.pricestr;
        
        CGRect rect = self.label.frame;
        rect.size.height = [[self class] heightForContent:timeMod.pricestr] + 30;
        self.label.frame = rect;
        
        
        CGRect viewBackRect = self.viewBack.frame;
        viewBackRect.size.height = self.label.frame.size.height;
        self.viewBack.frame = viewBackRect;
        
        // 设置左上角和右上角为圆角
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.viewBack.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(4, 4)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.viewBack.bounds;
        maskLayer.path = maskPath.CGPath;
        self.viewBack.layer.mask = maskLayer;
        [maskLayer release];
        
    } else {
        self.imageV.image = [UIImage imageNamed:@"detail_icon_position@2x.png"];
        self.imageV.frame = CGRectMake(9, (50 - 18) / 2.f, 18, 18);
        
        
        if (0 == timeMod.address.length) {
            self.label.text = @"未定, 待定";
        } else {
           self.label.text = timeMod.address;
        }
        CGRect rect = self.label.frame;
        rect.size.height = [[self class] heightForContent:self.label.text] + 30;
        self.label.frame = rect;
        
        CGRect viewBackRect = self.viewBack.frame;
        viewBackRect.size.height = self.label.frame.size.height;
        self.viewBack.frame = viewBackRect;
    }
    
    
    CGRect viewRect = self.view.frame;
    viewRect.origin.y = self.viewBack.frame.size.height - 1;
    self.view.frame = viewRect;
  
}
+ (CGFloat)getCellHeight:(NSIndexPath *)indexPath TimeMod:(ActivityTimeMod *)timeMod
{
    
    UIView *viewBack = [[UIView alloc] initWithFrame:CGRectMake(WIDTH, 0, SCREEN_WIDTH - WIDTH * 2, 50)];

    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(10, (50 - 16) / 2.f, 16, 16)];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(imageV.frame.origin.x + imageV.frame.size.width + 10, 0, viewBack.frame.size.width - (imageV.frame.size.width + imageV.frame.origin.x + 10 + 20), 49)];

    if (0 == indexPath.row) {
        
        label.text = timeMod.timestr;
        
        CGRect rect = label.frame;
        rect.size.height = [[self class] heightForContent:timeMod.timestr] + 30;
        label.frame = rect;

        
    } else if (2 == indexPath.row) {
        
        label.text = timeMod.pricestr;
        CGRect rect = label.frame;
        rect.size.height = [[self class] heightForContent:timeMod.pricestr] + 30;
        label.frame = rect;

    } else {

        imageV.frame = CGRectMake(9, (50 - 18) / 2.f, 18, 18);
        
        if (0 == timeMod.address.length) {
            label.text = @"未定, 待定";
        } else {
            label.text = timeMod.address;
        }
        CGRect rect = label.frame;
        rect.size.height = [[self class] heightForContent:label.text] + 30;
        label.frame = rect;

    }
    CGFloat height = label.frame.size.height;
    
    [viewBack release];
    [imageV release];
    [label release];
    
    return height;
}
+ (CGFloat)heightForContent:(NSString *)content
{
    CGSize size = CGSizeMake((SCREEN_WIDTH - WIDTH * 2) - (16 + 10 + 10 + 20), 1000);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16.f], NSFontAttributeName, nil];
    CGRect frame = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return (CGFloat)frame.size.height;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
